'use strict';
const Controller = require('egg').Controller;

class UsersController extends Controller {
  async login() {

    const ctx = this.ctx;

    const validateRule = {
      account: 'string',
      password: 'string',
    };

    ctx.validate(validateRule, ctx.request.body);

    const data = Object.keys(ctx.request.body).reduce((obj, key) => {
      obj[key] = ctx.request.body[key];
      return obj;
    }, {});

    const result = await ctx.service.users.login({ data });

    // 浏览器开发者工具的 status 状态
    ctx.body = result;
    ctx.status = 200;

  }

  async cookieOfLogin() {
    const ctx = this.ctx;

    const validateRule = {
      account: 'string',
      password: 'string',
    };

    ctx.validate(validateRule, ctx.request.body);

    const data = Object.keys(ctx.request.body).reduce((obj, key) => {
      obj[key] = ctx.request.body[key];
      return obj;
    }, {});

    const result = await ctx.service.users.cookieOfLogin({ data });

    ctx.body = result;
    ctx.status = 200;


  }
}

module.exports = UsersController;
